<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\facade\Builder;

use think\App;
use think\facade\View;
use think\response\Json;

class ActivityPhoto extends Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\ActivityPhoto';

    //关联模型
    protected array $hasOne = ['user', 'activity'];


    public function index(): string
    {
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'parent_code', 'title' => '活动名', 'modify' => '3,input', 'templet' => '<span>{{d.activity.title}}</span>','width' => 150])
            ->addColumn(['field' => 'u_id', 'title' => '会员', 'templet' => '#_userTpl', 'width' => 250])
            ->addColumn(['field' => 'images', 'title' => '缩略图', 'modify' => '2,img,1', 'templet' => '#imagesTpl'])
            ->addColumn(['field' => 'status', 'title' => '状态'])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
//            ->addTopButtons(['add' => ['size' => '1200,1200'], 'deleteAll'])
            ->addRightButtons(['edit' => ['title' => '查看', 'size' => '700,1000']])
            ->addSwitchTool()
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->addSearch([
                ['u_id', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ])
            ->fetch();
    }


    public function update(): Json
    {
        try {
            $param = $this->request->post();


            return json($this->model::_update($param, $this->getLogsArguments($param)));
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }


    public function edit($id = '', $tpl = 'activity_photo/edit', $info = ''): string
    {
        View::assign('form', ['title' => '上传图片', 'field' => 'images', 'value' => 1]);
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }


    public function save($param = [], $model = true): Json
    {
        try {
            $param = $this->request->post();
//            dump($param);die;
            return parent::save($param, $model); // TODO: Change the autogenerated stub
        } catch (\Exception $e) {
            dump($e->getMessage());die;

        }
    }


}
